package com.xyz.keshe;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.xyz.keshe.adapter.NewsAdapter;
import com.xyz.keshe.dao.CategoryDao;
import com.xyz.keshe.dao.NewsDao;
import com.xyz.keshe.model.Category;
import com.xyz.keshe.model.News;

import java.util.List;

public class CategoryDetailActivity extends AppCompatActivity {

    private TextView tvCategoryName;
    private TextView tvCategoryDescription;
    private ListView lvNews;
    private Button btnBack;
    private TextView tvNoData;

    private CategoryDao categoryDao;
    private NewsDao newsDao;
    private int categoryId;
    private Category category;
    private List<News> newsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_detail);

        // 初始化视图
        tvCategoryName = findViewById(R.id.tv_category_name);
        tvCategoryDescription = findViewById(R.id.tv_category_description);
        lvNews = findViewById(R.id.lv_news);
        btnBack = findViewById(R.id.btn_back);
        tvNoData = findViewById(R.id.tv_no_data);

        // 初始化DAO
        categoryDao = new CategoryDao(this);
        newsDao = new NewsDao(this);

        // 获取栏目ID
        Intent intent = getIntent();
        categoryId = intent.getIntExtra("category_id", 0);
        if (categoryId == 0) {
            Toast.makeText(this, "栏目ID无效", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 获取栏目详情
        category = categoryDao.getCategoryById(categoryId);
        if (category == null) {
            Toast.makeText(this, "栏目不存在", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 设置栏目详情
        tvCategoryName.setText(category.getName());
        tvCategoryDescription.setText(category.getDescription());

        // 获取栏目下的新闻列表
        newsList = newsDao.getNewsByCategoryId(categoryId);
        NewsAdapter newsAdapter = new NewsAdapter(this, newsList);
        lvNews.setAdapter(newsAdapter);

        // 设置新闻点击事件
        lvNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                News news = (News) parent.getItemAtPosition(position);
                // 跳转到新闻详情页
                Intent newsIntent = new Intent(CategoryDetailActivity.this, NewsDetailActivity.class);
                newsIntent.putExtra("news_id", news.getId());
                startActivity(newsIntent);
            }
        });

        // 设置返回按钮点击事件
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 显示或隐藏无数据提示
        if (newsList.isEmpty()) {
            tvNoData.setVisibility(View.VISIBLE);
            lvNews.setVisibility(View.GONE);
        } else {
            tvNoData.setVisibility(View.GONE);
            lvNews.setVisibility(View.VISIBLE);
        }
    }
} 